package com.yeyks.rabbitmq.rabbitlistener;

import com.alibaba.fastjson.JSONObject;
import com.yeyks.constant.ConfigKey;
import com.yeyks.dal.domain.ActivityInfo;
import com.yeyks.dal.domain.MessageNotice;
import com.yeyks.getui.service.GeTuiService;
import com.yeyks.rabbitmq.config.MqQueueConfig;
import com.yeyks.service.ConfigService;
import com.yeyks.service.MessageNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fuchangshuai
 * @date 2019/4/29 0029
 */
@Component
@Slf4j
public class GetuiQueueListener {

    @Autowired
    private GeTuiService geTuiService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private MessageNoticeService noticeService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqQueueConfig.ORDER_QUEUE_GETUI, durable = "true"),
            exchange = @Exchange(value = MqQueueConfig.ORDER_GETUI_EXCHANGE, durable = "true", type = "topic"),
            key = MqQueueConfig.ORDER_GETUI_KEY))
    public void getuiPushMessage(ActivityInfo activityInfo) {
        try {
            log.info("个推mq监听到的消息为{}", JSONObject.toJSONString(activityInfo));
            if (activityInfo == null) {
                log.info("个推mq监听到的消息为null");
                return;
            }
            String title = activityInfo.getTitle();
            String content = activityInfo.getContent();
            String url = null;
            if (activityInfo.getType().equals(1)) {
                url = activityInfo.getUrlOrText();
            }
            if (activityInfo.getType().equals(2)) {
                url  = getActivityUrl(activityInfo);
            }
            Map<String, String> map = getMap(title, content, url);
            if (activityInfo.getId() != null && title != null && content != null) {
                createMessage(activityInfo);
                geTuiService.allPush(title, JSONObject.toJSONString(map));
            }
        } catch (Exception e) {
            log.error("个推mq消息发送异常{}", JSONObject.toJSONString(activityInfo));
        }
    }

    private void createMessage(ActivityInfo activityInfo) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("content", activityInfo.getContent());
            map.put("iconUrl", getActivityIcon());
            map.put("title", activityInfo.getTitle());
            noticeService.activieyIdCreateAndSave(map, activityInfo.getId().intValue(), MessageNotice.Type.activity_activity.value());
        } catch (Exception e) {
            log.error("创建活动消息失败异常{}", e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    private String getActivityIcon() {
        try {
            String value = configService.getValue(ConfigKey.ACTIVITY_ICON);
            if (value != null) {
                return value;
            }
            return "http://yeyu-kuaisu.oss-cn-hangzhou.aliyuncs.com/Public/testicon.png";

        } catch (Exception e) {
            log.error("个推发送消息获取活动icon异常{}", e.getLocalizedMessage());
            e.printStackTrace();
            return "http://yeyu-kuaisu.oss-cn-hangzhou.aliyuncs.com/Public/testicon.png";
        }
    }

    private String getActivityUrl(ActivityInfo activityInfo) {
        try {
            String value = configService.getValue(ConfigKey.ACTIVITY_URL);
            if (value != null) {
                return value + activityInfo.getId();
            }
            return "https://kuaisutest.chengguokj.com/h5";

        } catch (Exception e) {
            log.error("个推发送消息获取链接异常{}", e.getLocalizedMessage());
            e.printStackTrace();
            return "https://kuaisutest.chengguokj.com/h5";
        }
    }

    private Map<String, String> getMap(String title, String content, String activityId) {
        Map<String, String> map = new HashMap<>();
        map.put("title", title);
        map.put("url", activityId);
        map.put("content", content);
        return map;
    }
}
